package com.yhp.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yhp.bo.CommentBO;
import com.yhp.bo.UsersBO;
import com.yhp.enums.CommentLevel;
import com.yhp.enums.Sex;
import com.yhp.enums.YesOrNo;
import com.yhp.mapper.*;
import com.yhp.pojo.*;
import com.yhp.service.BaseService;
import com.yhp.service.ItemService;
import com.yhp.service.UserService;
import com.yhp.utils.DateUtil;
import com.yhp.utils.DesensitizationUtil;
import com.yhp.utils.MD5Utils;
import com.yhp.utils.PagedGridResult;
import com.yhp.vo.CommentCountsVO;
import com.yhp.vo.ItemContentVO;
import com.yhp.vo.SearchItemVO;
import com.yhp.vo.ShopCartVO;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ItemServiceImpl extends BaseService implements ItemService  {

    @Autowired
    private ItemsMapper  itemsMapper;

    @Autowired
    private ItemsImgMapper itemsImgMapper;
    @Autowired
    private ItemsSpecMapper itemsSpecMapper;
    @Autowired
    private ItemsParamMapper itemsParamMapper;

    @Autowired
    private ItemsCommentsMapper itemsCommentsMapper;

    @Autowired
    private ItemsMapperCustom itemsMapperCustom;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Items queryItemById(String id) {

        return itemsMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<ItemsImg> getImages(String id) {
        Example example = new Example(ItemsImg.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("itemId",id);
        List<ItemsImg> itemsImgs = itemsImgMapper.selectByExample(example);
        return itemsImgs;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<ItemsSpec> getSpec(String id) {
        Example example = new Example(ItemsSpec.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("itemId",id);
        List<ItemsSpec> itemsSpecs = itemsSpecMapper.selectByExample(example);
        return itemsSpecs;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public ItemsParam getParam(String id) {
        Example example = new Example(ItemsParam.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("itemId",id);
        ItemsParam itemsParam = itemsParamMapper.selectOneByExample(example);
        return itemsParam;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public CommentCountsVO queryComments(String itemId) {
        //好评数
        Integer goodCount = getCommentCount(itemId, CommentLevel.GOOD.type);
        //中评数
        Integer normalCount = getCommentCount(itemId, CommentLevel.NORMAL.type);
        //差评数
        Integer badCount = getCommentCount(itemId, CommentLevel.BAD.type);
        //总评价书
        Integer total=goodCount+normalCount+badCount;

        CommentCountsVO commentCountsVO = new CommentCountsVO();
        commentCountsVO.setTotalCounts(total);
        commentCountsVO.setGoodCounts(goodCount);
        commentCountsVO.setNormalCounts(normalCount);
        commentCountsVO.setBadCounts(badCount);
        return commentCountsVO;
    }

    //私有方法:根据评价等级查询评价数量
    //数据库有好评，中评，差评，没有总评，总评使用函数计算出总和
    @Transactional(propagation = Propagation.SUPPORTS)
     Integer getCommentCount(String itemId,Integer level){
        ItemsComments itemsComments = new ItemsComments();
        itemsComments.setItemId(itemId);
        if(level!=null){
            itemsComments.setCommentLevel(level);
        }

        return itemsCommentsMapper.selectCount(itemsComments);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public PagedGridResult queryContentsList(CommentBO commentBO) {
        PageHelper.startPage(commentBO.getPage(),commentBO.getPageSize());

        Map map=new HashMap();
        map.put("itemId",commentBO.getItemId());
        map.put("level",commentBO.getLevel());
        List<ItemContentVO> itemContentVOS = itemsMapperCustom.queryComments(map);
        //给评价人姓名脱敏
        for (ItemContentVO itemContentVO : itemContentVOS) {
            String name=itemContentVO.getNickname();
            String s = DesensitizationUtil.commonDisplay(name);
            itemContentVO.setNickname(s);
        }


        return setPagedGrid(itemContentVOS,commentBO.getPage());
    }


    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public PagedGridResult searchItems(String keyWord, String sort, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);

        Map map=new HashMap();
        map.put("keyword",keyWord);
        map.put("sort",sort);
        List<SearchItemVO> voList = itemsMapperCustom.queryItem(map);

        return setPagedGrid(voList,page);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public PagedGridResult searchCatItems(String catId, String sort, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);

        Map map=new HashMap();
        map.put("catId",catId);
        map.put("sort",sort);
        List<SearchItemVO> voList = itemsMapperCustom.queryCatItems(map);


        return setPagedGrid(voList,page);
    }



    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<ShopCartVO> queryItemsBySpecIds(String specids) {
        String[] split = specids.split(",");
        List arrayList = new ArrayList();
        Collections.addAll(arrayList, split);//将数组中的数据添加到集合中
        return itemsMapperCustom.queryItemsBySpecIds(arrayList);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public ItemsSpec queryByItemSpecId(String specId) {
        return itemsSpecMapper.selectByPrimaryKey(specId);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public ItemsImg queryItemMainImgById(String itemId) {
        ItemsImg itemsImg = new ItemsImg();
        itemsImg.setItemId(itemId);
        itemsImg.setIsMain(YesOrNo.YES.type);
        return itemsImgMapper.selectOne(itemsImg);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateStock(String specid, Integer count) {
        //注意“超卖”问题-分布式锁问题
        //synchronized 不推荐使用，集群下无用，性能低下
        //锁数据库  不推荐，导致数据库性能低下
        //分布式锁  zookeeper,redis
        //单体情况下，可以选择使用乐观锁
        Map map=new HashMap();
        map.put("buycount",count);
        map.put("specid",specid);
        int i = itemsMapperCustom.updateStock(map);
        if(i!=1){
            throw new RuntimeException("订单创建失败，原因:商品库存不足");
        }
    }
}
